﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Compilex.Syntatic.Declarations;
using Compilex.Syntatic.Descriptors;
using Infraestruturex.BaseLibrary;

namespace Compilex.Syntatic
{
    public class FunctionCallStack
    {
        public SyntaticContext Context { get; set; }
        private SimpleStack<FunctionCall> functionsCalled = new SimpleStack<FunctionCall>();

        public FunctionCallStack(SyntaticContext context)
        {
            this.Context = context;
            this.functionsCalled = new SimpleStack<FunctionCall>();
        }

        public void StartFunctionCall()
        {
            string functionName = Context.SyntaticStack.Pop();
            SymbolTableHelper.AssertFunctionIdentifierDeclared(functionName, Context);

            FunctionDescriptor descriptor = (FunctionDescriptor)Context.ScopeManager.CurrentScope.
                GetDeclarationAnywhere(SymbolClass.Funcex, functionName).Descriptor;

            if (!descriptor.IsLibraryFunction)
            {
                functionsCalled.Push(new FunctionCall(Context) { FunctionName = functionName });
            }
            else
            {
                functionsCalled.Push(new LibraryFunctionCall(Context) { FunctionName = functionName });
            }
        }



        public FunctionCall Current { get { return functionsCalled.Peek(); } }

        public void ReadBExpressionParameter()
        {
            Current.ReadBExpressionParameter();
        }

        public void ReadLiteralParameter()
        {
            Current.ReadLiteralParameter();
        }

        public void EndFunctionCall()
        {
            FunctionCall functionCall = functionsCalled.Pop();
            functionCall.EndFunctionCall();
        }
     }
}
